package com.ca.project.Server.Modules.RepairInfo.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ca.project.Server.Modules.RepairInfo.Context.AcceptRepairInfoContext;
import com.ca.project.Server.Modules.RepairInfo.Context.CreateRepairInfoContext;
import com.ca.project.Server.Modules.RepairInfo.Converter.RepairInfoConverter;
import com.ca.project.Server.Modules.RepairInfo.DTO.RepairInfoDTO;
import com.ca.project.Server.Modules.RepairInfo.Entity.RepairInfo;
import com.ca.project.Server.Modules.RepairInfo.PO.AcceptRepairInfoPO;
import com.ca.project.Server.Modules.RepairInfo.PO.CreateRepairInfoPO;
import com.ca.project.Server.Modules.RepairInfo.Service.RepairInfoService;
import com.ca.project.Server.Modules.RepairInfo.VO.*;
import com.ca.project.core.Respond.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @PACKAGE_NAME: com.ca.project.Server.Modules.RepairInfo.Controller
 * @NAME: RepairInfoController
 * @USER: ASUS
 * @DATE: 3/1/2025
 * @TIME: 下午3:22
 **/
@RestController
@RequestMapping("/repair")
@Api(tags = "维修订单模块")
public class RepairInfoController {

    @Autowired
    private RepairInfoConverter repairInfoConverter;
    @Autowired
    private RepairInfoService repairInfoService;

    @ApiOperation(
            value = "维修订单创建接口",
            notes = "该接口提供了维修订单创建的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/create")
    public R RepairInfoCreate(@Validated @RequestBody CreateRepairInfoPO createRepairInfoPO) {
        CreateRepairInfoContext createRepairInfoContext =
                repairInfoConverter.CreateRepairInfoPO2CreateRepairInfoContext(createRepairInfoPO);
        repairInfoService.create(createRepairInfoContext);
        return R.success();
    }

    @ApiOperation(
            value = "维修订单删除接口",
            notes = "该接口提供了维修订单删除的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/delete")
    public R DeleteRepairInfo(@Validated @RequestBody DeleteRepairInfoVO deleteRepairInfoVO) {
        String r_id = deleteRepairInfoVO.getId();
        repairInfoService.delete(r_id);
        return R.success();
    }

    @ApiOperation(
            value = "维修订单被维修人员接单接口",
            notes = "该接口提供了维修订单被维修人员接单的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/accept")
    public R RepairInfoAccept(@Validated @RequestBody AcceptRepairInfoPO acceptRepairInfoPO) {
        AcceptRepairInfoContext acceptRepairInfoContext =
                repairInfoConverter.AcceptRepairInfoPO2AcceptRepairInfoContext(acceptRepairInfoPO);
        repairInfoService.accept(acceptRepairInfoContext);
        return R.success();
    }

    @ApiOperation(
            value = "维修订单待处理接口",
            notes = "该接口提供了维修订单待处理的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/pending")
    public R PendingRepairInfo(@Validated @RequestBody PendingRepairInfoVO pendingRepairInfoVO) {
        String token = pendingRepairInfoVO.getToken();
        List<RepairInfo> entity = repairInfoService.getPending(token);
        return R.data(entity);
    }

    @ApiOperation(
            value = "维修订单已完成接口",
            notes = "该接口提供了维修订单已完成的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/finish")
    public R FinishRepairInfo(@Validated @RequestBody FinishRepairInfoVO finishRepairInfoVO) {
        String token = finishRepairInfoVO.getToken();
        List<RepairInfo> entity = repairInfoService.getFinish(token);
        return R.data(entity);
    }

    @ApiOperation(
            value = "维修订单已接单接口",
            notes = "该接口提供了维修订单已接单的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/accept_info")
    public R acceptRepairInfo(@Validated @RequestBody AcceptRepairInfoVO acceptRepairInfoVO) {
        String token = acceptRepairInfoVO.getToken();
        List<RepairInfo> entity = repairInfoService.getAccept(token);
        return R.data(entity);
    }

    @ApiOperation(
            value = "维修订单接口",
            notes = "该接口提供了维修订单已完成的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/finish_repair")
    public R RepairInfoFinish(@Validated @RequestBody AcceptRepairInfoPO acceptRepairInfoPO) {
        AcceptRepairInfoContext acceptRepairInfoContext =
                repairInfoConverter.AcceptRepairInfoPO2AcceptRepairInfoContext(acceptRepairInfoPO);
        repairInfoService.finish(acceptRepairInfoContext);
        return R.success();
    }


    @ApiOperation(
            value = "查看用户维修订单接口",
            notes = "该接口提供了查看用户维修订单的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/info_list")
    public R FinishRepairInfo(@Validated @RequestBody RepairInfoVO repairInfoVO) {
        String token = repairInfoVO.getToken();
        List<RepairInfo> entity = repairInfoService.getInfo(token);
        return R.data(entity);
    }

    @ApiOperation(
            value = "查看特定用户维修订单接口",
            notes = "该接口提供了查看用户维修订单的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/info")
    public R FinishRepairInfo(@Validated @RequestBody SpecificRepairInfoVO specificRepairInfoVO) {
        String id = specificRepairInfoVO.getR_id();
        RepairInfoDTO entity = repairInfoService.getSpecificInfo(id);
        return R.data(entity);
    }

    @ApiOperation(
            value = "查看用户维修订单-分页接口",
            notes = "该接口提供了查看用户维修订单-分页的功能",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @GetMapping("/{currentPage}/{PageSize}")
    public R GetAllRepairInfo(@PathVariable Integer currentPage, @PathVariable Integer PageSize, RepairInfo repairInfo) {
        IPage<RepairInfo> page = repairInfoService.getPage(currentPage, PageSize, repairInfo);
        if (currentPage > page.getPages())
            page = repairInfoService.getPage((int) page.getPages(), PageSize);
        return R.data(page);
    }
}
